<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_store_order\Entity\ShusyStoreOrder;
use Drupal\shusy_store_order_line\Entity\ShusyStoreOrderLine;

/**
 * @OA\Schema(
 *   schema="OrderCart",
 *   type="object",
 *   description="OrderCart"
 * )
 */
class OrderCart extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_store_order';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyStoreOrder) {
      $qty = 0;
      $isAllService = TRUE;
      $products = [];
      $es = shusy_store_order_line_load_multiple(FALSE, [
        'store_order_id' => $this->entity->id,
        'status' => 1,
      ]);
      foreach ($es as $e) {
        if ($e instanceof ShusyStoreOrderLine) {
          switch ($e->type) {
            case 'product':
              $lineWrapper = $e->wrapper();
              $is_service = (bool) $lineWrapper->standard->product->is_service->value();
              if (!$is_service) {
                $isAllService = FALSE;
              }
              $products[] = LineItemProduct::get($e->id);
              $qty += $e->qty;
              break;
          }
        }
      }
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="store",
         *   type="object",
         *   ref="#/components/schemas/StoreItem",
         * ),
         */
        'store' => StoreItem::get($this->entity->store_id),
        /**
         * @OA\Property(
         *   property="products",
         *   type="array",
         *   description="产品列表",
         *   @OA\Items(ref="#/components/schemas/LineItemProduct")
         * ),
         */
        'products' => $products,
        /**
         * @OA\Property(
         *   property="qty",
         *   type="integer",
         *   description="数量",
         * ),
         */
        'qty' => (int) $qty,
        /**
         * @OA\Property(
         *   property="type",
         *   type="integer",
         *   description="订单类型 0 正常 1 拼团 2 秒杀",
         * ),
         */
        'type' => (int) $this->entity->type,
        /**
         * @OA\Property(
         *   property="isService",
         *   type="boolean",
         *   description="购买的全部是服务",
         * ),
         */
        'isService' => $isAllService,
      ];
      $this->merge($value);
    }
  }
}
